<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="600px"
    top="5%"
  >
    <t-form ref="form" :data="formData" labelWidth="100px" :rules="rules">
      <t-form-item label="指南名称" name="guideName">
        <t-input clearable v-model="formData.guideName" :maxLength="100" placeholder="请输入指南名称"></t-input>
      </t-form-item>
      <t-form-item label="所属年度" name="guideYear">
        <t-date-picker v-model="formData.guideYear" mode="year" clearable/>
      </t-form-item>
      <t-form-item label="附件上传" name="sysAttMainIds">
        <file-upload :fileList="fileLists" v-model="formData.sysAttMainVo.sysAttMainIds"></file-upload>
      </t-form-item>
      <t-form-item label="备注" name="note">
        <t-textarea
          v-model="formData.note"
          placeholder="请输入内容"
        />
      </t-form-item>
    </t-form>
  </t-dialog>
</template>

<script>
import FileUpload from "@/components/FileUpload/index.vue"
import { getFiles } from "@/api/common";

export default {
  name: "add-edit-guide",
  components:{
    FileUpload
  },
  props: ["header", "objId"],
  data() {
    return {
      visible: true,
      loading: false,
      fileLists: [],
      formData: {
        guideName: "",
        guideYear: "",
        note: "",
        sysAttMainVo:{
          sysAttMainIds:[]
        }
      },
      rules: {
        guideName: [{ required: true, message: "指南名称不能为空"}],
        guideYear: [{ required: true, message: "指南年度不能为空"}],
      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    }
  },
  methods: {
    getObj() {
      this.$api.kjxm.project.guideGet(this.objId).then((response) => {
        response.data.sysAttMainVo = response.data.sysAttMainVo || {sysAttMainIds:[]}
        this.formData = response.data;
        getFiles(this.objId).then((res) => {
          this.fileLists = []
          const ids = [];
          if (res.data && res.data.length) {
            res.data.forEach((item) => {
              ids.push(item.id);
              this.fileLists.push({ name: item.fileName, url: item.fullFilePath, id: item.id, filePath: item.filePath })
            })
            this.formData.sysAttMainVo.sysAttMainIds = ids;
          }
        });
      });
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId !== 0) {
            this.$api.kjxm.project.guideEdit(this.formData).then((response) => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.kjxm.project.guideAdd(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
  }
}
</script>

<style scoped>

</style>
